package com.reactnativedemo;

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import java.util.Arrays;
import java.util.List;

import com.mobileClientKernel.android.commonLayer.FileOperator;
import com.mobileClientKernel.android.reactNative.bridge.JJRNPackage;
import com.mobileClientKernel.android.serviceLayer.httpTransportService.urlconfig.JJTUrlConfigManager;
import com.mobileClientKernel.android.serviceManager.servicefactory.JJServiceFactory;

import org.json.JSONException;
import org.json.JSONObject;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
              new MainReactPackage(), new JJRNPackage()
      );
    }

    @Override
    protected String getJSMainModuleName() {
      return "index";
    }

    /*
      @Nullable
      @Override
      protected String getJSBundleFile() {
          String version =UniqueIDUtil.getVersionName(getMyApplicationContext()) ;//3.2.0

          String hotfixVersion=PreferenceUtil.getString(PreferenceUtil.HOTFIXVERSION,getMyApplicationContext());//3.2.0.1
          String hotfixNewVersion=PreferenceUtil.getString(PreferenceUtil.HOTFIXVERSIONNEW,getMyApplicationContext());//3.2.0.2
          try {
              if (TextUtils.isEmpty(hotfixNewVersion)){//预更新版本为空
                  if (TextUtils.isEmpty(hotfixVersion)){//当前热更新版本为空
                      PreferenceUtil.putString(PreferenceUtil.HOTFIXVERSION,"",getMyApplicationContext());
                      return super.getJSBundleFile();
                  }else {//当前热更新版本不为空
                      return getJSBundle(version,hotfixVersion);
                  }
              }else {////预更新版本不为空
                  return getJSBundle(version,hotfixNewVersion);
              }


          } catch (Exception e) {

              JSONObject object= new JSONObject();
              JSONUtil.put(object,"原因","原因 :"+e);
              ZhugeSDK.getInstance().track(MainApplication.getInstance(),"RN热更新-加载失败",object);
              e.printStackTrace();
              TLog._e("getJSBundleFile8",super.getJSBundleFile());
              PreferenceUtil.putString(PreferenceUtil.HOTFIXVERSION,"",getMyApplicationContext());
              PreferenceUtil.putString(PreferenceUtil.HOTFIXVERSIONNEW,"",getMyApplicationContext());
              return super.getJSBundleFile();
          }
      }
      */
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);

    JJServiceFactory.getInstance().setContext(getApplicationContext());

    String jsonString = FileOperator.readAssetFile(getApplicationContext(), "JJUrlConfig.json");
    try {
      JSONObject urlConfigObject = new JSONObject(jsonString);
      JJTUrlConfigManager.getInstance().addUrlConfig(urlConfigObject);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

    /**
     * 判断文件存在并且MD5值相等
     * @param hotfixVersion
     * @return
     */
    /*
    private boolean fileExistAndMd5Equals(String hotfixVersion){
        String filePath = Global.HOTFIXPATH + "/snc_rn_android_"+hotfixVersion+"/"+Global.JS_BUNDLE_LOCAL_FILE;
        File file = new File(filePath);
        boolean fileExists= file.exists();
        String filePutMd5 =  PreferenceUtil.getString(hotfixVersion,getMyApplicationContext());
        if (fileExists){
            String fileMd5 =  MD5Util.getFileMD5(file);
            if (filePutMd5.equals(fileMd5)){
                return true;
            }else {
                JSONObject object= new JSONObject();
                JSONUtil.put(object,"原因","原因 :MD5不一致");
                ZhugeSDK.getInstance().track(MainApplication.getInstance(),"RN热更新-加载失败",object);
                return false;
            }
        }else {
            JSONObject object= new JSONObject();
            JSONUtil.put(object,"原因","原因 :文件不存在");
            ZhugeSDK.getInstance().track(MainApplication.getInstance(),"RN热更新-加载失败",object);
            return false;
        }


    }
*/
    /**
     * 判断version 与热更新版本大小关系
     * @param version
     * @param hotfixVersion
     * @return
     * @throws Exception
     */
    /*
    private String getJSBundle(String version,String hotfixVersion) throws Exception {
        if (CommonUtils.compareVersion(version,hotfixVersion)>0){//判断version,hotfixVersion大小
            TLog._e("getJSBundleFile5","null");
            PreferenceUtil.putString(PreferenceUtil.HOTFIXVERSION,"",getMyApplicationContext());
            CommonUtils.deleteFileDirectory(new File(Global.HOTFIXPATH));
            return null;
        }else {
            if (fileExistAndMd5Equals(hotfixVersion)){//hotfixVersion是否存在，获取bundle路径
                JSONObject object= new JSONObject();
                JSONUtil.put(object,"版本号","版本号 :"+hotfixVersion);
                ZhugeSDK.getInstance().track(MainApplication.getInstance(),"RN热更新-加载成功",object);
                PreferenceUtil.putString(PreferenceUtil.HOTFIXVERSION,hotfixVersion,getMyApplicationContext());
                return Global.HOTFIXPATH+"/snc_rn_android_"+hotfixVersion+"/"+Global.JS_BUNDLE_LOCAL_FILE;
            }else {
                PreferenceUtil.putString(PreferenceUtil.HOTFIXVERSION,"",getMyApplicationContext());
                return null;
            }

        }
    }
    */
}
